package lx.exalarm.alarm;

import android.app.AlarmManager;
import android.app.Application;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.widget.Toast;

import lx.exalarm.R;
import lx.exalarm.activity.ActivityPlayAudio;
import lx.exalarm.activity.ActivityPlayImage;
import lx.exalarm.activity.ActivityPlayVideo;
import lx.exalarm.app.ThisApp;
import lx.exalarm.base.BasePlayActivity;
import lx.exalarm.utils.Time;
import lx.exalarm.utils.log.Log;

/**
 * Created by liuxu on 15-2-27.
 */
public final class AlarmUtils {

    private static Application sApp = ThisApp.getInstance();

    public static final String ACTION_ALARM = "lx.exalarm.ACTION_ALARM";
    public static final String ACTION_ALARM_ACT = "lx.exalarm.ACTION_ALARM_ACT";

    public static final String EXTRA_ALARM_ID = "lx.exalarm.EXTRA_ALARM_ID";
    public static final String EXTRA_ACTION_ID = "lx.exalarm.EXTRA_ACTION_ID";

    private AlarmUtils() {}

    /**
     * set or cancel alarm according to AlarmBean.isEnabled.
     * @param alarm alarm
     */
    public static void updateAlarm(AlarmBean alarm) {
        Log.d("AlarmUtils.updateAlarm, alarm=" + alarm);
        Intent intent = new Intent(ACTION_ALARM);
        intent.putExtra(EXTRA_ALARM_ID, alarm.getId());
        PendingIntent pi = PendingIntent.getBroadcast(
                sApp, alarm.getId(), intent, PendingIntent.FLAG_UPDATE_CURRENT);
        AlarmManager am = (AlarmManager) sApp.getSystemService(Context.ALARM_SERVICE);
        if (alarm.isEnabled()) {
            long current = System.currentTimeMillis();
            long start = alarm.getStartTime();
            Log.d("AlarmUtils.updateAlarm, current=" + current + ", start=" + start);
            am.set(AlarmManager.RTC_WAKEUP, start, pi);
            showTimeToast(start - current);
            //am.set(AlarmManager.RTC_WAKEUP, current + 20 * 1000, pi);
        } else {
            am.cancel(pi);
        }
    }

    /**
     * set operation contained by action to AlarmManager.
     * @param action action
     */
    public static void startAction(ActionBean action) {
        Log.d("AlarmUtils.startAction, action=" + action);
        if (action == null) {
            return;
        }
        Intent intent = new Intent(ACTION_ALARM_ACT);
        intent.putExtra(EXTRA_ACTION_ID, action.getId());
        PendingIntent pi = PendingIntent.getBroadcast(
                sApp, action.getId(), intent, PendingIntent.FLAG_UPDATE_CURRENT);
        AlarmManager am = (AlarmManager) sApp.getSystemService(Context.ALARM_SERVICE);
        long time = action.getStartTime();
        if (time == 0) {
            // do not start too soon if interval is zero
            time = 3 * 1000;
        }
        am.set(AlarmManager.RTC_WAKEUP, time, pi);
        //am.set(AlarmManager.RTC_WAKEUP, System.currentTimeMillis() + 5 * 1000, pi);
    }

    /**
     * start activity according to action.
     * @param action action
     */
    public static void startActivity(ActionBean action) {
        switch (action.getType()) {
            case ActionBean.TYPE_AUDIO: {
                BasePlayActivity.start(ActivityPlayAudio.class, action);
                break;
            }
            case ActionBean.TYPE_VIDEO: {
                BasePlayActivity.start(ActivityPlayVideo.class, action);
                break;
            }
            case ActionBean.TYPE_IMAGE: {
                BasePlayActivity.start(ActivityPlayImage.class, action);
                break;
            }
        }
    }

    /**
     * show a toast to display when the alarm will run
     * @param millis time from now
     */
    private static void showTimeToast(long millis) {
        final Context cxt = ThisApp.getInstance();
        final Time time = new Time(millis);
        final int day = time.get(Time.DAY);
        final int hour = time.get(Time.HOUR);
        final int minute = time.get(Time.MINUTE);

        StringBuilder sb = new StringBuilder();
        if (day != 0) {
            sb.append(cxt.getString(R.string.toast_alarm_time_day, day)).append(" ");
        }
        if (hour != 0) {
            sb.append(cxt.getString(R.string.toast_alarm_time_hour, hour)).append(" ");
        }
        sb.append(cxt.getString(R.string.toast_alarm_time_minute, minute));

        String msg = cxt.getString(R.string.toast_alarm_time, sb.toString());
        Log.d("AlarmUtils.showTimeToast, alarm time: " + msg);
        Toast.makeText(cxt, msg, Toast.LENGTH_LONG).show();
    }
}
